package 中等.模拟;

/**
 * 编写一种算法，若M × N矩阵中某个元素为0，则将其所在的行与列清零。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/zero-matrix-lcci/
 */
public class 零矩阵_0108 {

    public static void main(String[] args) {

    }

    /**
     * 数组标记出现 0 的行和列
     *
     * @param matrix
     */
    public void setZeroes(int[][] matrix) {
        boolean[] rowRecord = new boolean[matrix.length];
        boolean[] columnRecord = new boolean[matrix[0].length];

        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix[0].length; column++) {
                if (matrix[row][column] == 0) {
                    rowRecord[row] = true;
                    columnRecord[column] = true;
                }
            }
        }
        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix[0].length; column++) {
                if (rowRecord[row] || columnRecord[column]) {
                    matrix[row][column] = 0;
                }
            }
        }
    }

}
